package com.bookmall.cart.config;

import com.bookmall.cart.domain.OrderInfo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-20-21:52
 */
@Configuration
public class RabbitConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 创建两个队列
     * @author 胡菜鸡
     * @date 2022/4/26 12:00
     */
    @Bean
    public Queue orderDelayQueue(){
        //设定延迟队列参数
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 300000);

        Queue queue = new Queue("order.delay.queue", true, false ,false, arguments);
        return queue;
    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue("order.release.order.queue");
        return queue;
    }

    /**
     * 创建一个交换机
     * @author 胡菜鸡
     * @date 2022/4/26 12:00
     */
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange");
    }

    /**
     * 创建两个绑定关系
     * @author 胡菜鸡
     * @date 2022/4/26 12:00
     */
    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 更改存储方式为JSON存储
     * @author 胡菜鸡
     * @date 2022/4/21 13:39
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 发送端的消息确认机制
     * @author 胡菜鸡
     * @date 2022/4/21 14:07
     */
    @PostConstruct  // 对象创建完成后，执行这个方法
    public void initRabbitTemplate(){
        //设置确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             * 只要消息成功抵达，参数ack就为true
             * @author 胡菜鸡
             * @date 2022/4/21 13:43
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("confirm...[" + correlationData + "]==>ask[" + ack + "]==>cause[" + cause + "]");
            }
        });

        //设置失败回调
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                System.out.println("returned...[" + returned + "]");
            }
        });
    }


}
